﻿@{
    ViewBag.Title = "GroupUser";
}
<input id="gid" value="@ViewBag.GID" class="mini-hidden" />
<div style="width: 100%; height: 100%">
    <div class="mini-splitter" style="width: 100%; height: 100%;">
        <div size="260" showcollapsebutton="true">
            <div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
                <input id="unSelectedKey" class="mini-textbox" style="width: 140px; padding-left: 2px;" emptytext="请输入关键字" onenter="onKeyEnter" />
                <a class="mini-button" iconcls="icon-search" plain="true" onclick="searchUnSelected()" tooltip="搜索"></a>
                <span class="separator"></span>
                <a class="mini-button" iconcls="icon-add" onclick="add()" plain="true">添加</a>
            </div>
            <div class="mini-fit">
                <ul id="tree" class="mini-tree" style="width: 200px; padding: 5px;" resultastree="false"
                    showtreeicon="true" textfield="UName" idfield="UID" parentfield="FID" iconfield="Icon"
                    showcheckbox="true" checkrecursive="true" autocheckparent="true" expandonload="true"
                    allowselect="false" enablehottrack="false">
                </ul>
            </div>
        </div>
        <div showcollapsebutton="true">
            <div class="mini-toolbar" style="border-bottom: 0; padding: 0px;">
                <table style="width: 100%;">
                    <tr>
                        <td style="width: 100%;">
                            <a class="mini-button" iconcls="icon-remove" onclick="remove()" plain="true">移除</a>
                        </td>
                        <td style="white-space: nowrap;">
                            <input id="username" class="mini-textbox" emptytext="请输入关键字" style="width: 150px;" onenter="onSKeyEnter" />
                            <a class="mini-button" iconcls="icon-search" onclick="searchSelected()" plain="true" tooltip="搜索"></a>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="mini-fit">
                <div id="grid" class="mini-datagrid" style="width: 100%; height: 100%;" allowresize="false"
                    idfield="UserCode" multiselect="true" showemptytext="true" emptytext="无记录">
                    <div property="columns">
                        <div type="checkcolumn" width="40"></div>
                        <div field="UName" width="150px" headeralign="center">用户名</div>
                        <div field="CName" width="100%" headeralign="center">公司名</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    mini.parse();

    var tree = mini.get("tree");
    tree.setUrl("/SystemData/Group/GetUnSelectedUser?gid=" + mini.get("gid").getValue());

    var grid = mini.get("grid");
    grid.setUrl("/SystemData/Group/GetSelectedUser?gid=" + mini.get("gid").getValue());
    grid.load({ key: mini.get("username").getValue() });

    function onKeyEnter() {
        searchUnSelected();
    }

    function searchUnSelected() {
        var key = mini.get("unSelectedKey").getValue();
        if (key == "") {
            tree.clearFilter();
        } else {
            key = key.toLowerCase();
            tree.filter(function (node) {
                var text = node.UName ? node.UName.toLowerCase() : "";
                if (text.indexOf(key) != -1) {
                    return true;
                }
            });
        }
    }

    function onSKeyEnter() {
        searchSelected();
    }

    function searchSelected() {
        grid.setUrl("/SystemData/Group/GetSelectedUser?gid=" + mini.get("gid").getValue());
        grid.load({ key: mini.get("username").getValue() });
    }

    function add() {
        var nodes = tree.getCheckedNodes();
        if (!nodes) {
            $.tip({ text: "请选择至少一名用户", staytime: 1000 });
            return;
        }
        var UG = '[';
        for (var i = 0, l = nodes.length; i < l; i++) {
            if (nodes[i].CType == "U") {
                UG += '{"UID":"' + nodes[i].UID + '","GID":"' + mini.get("gid").getValue() + '","_state":"added"},';
            }
        }
        if (UG.length > 1) {
            UG = UG.substr(0, UG.length - 1);
        }
        UG += ']';

        var add = mini.encode(UG);

        grid.loading("正在保存");
        $.ajax({
            url: "/SystemData/Group/SaveGroupUser",
            type: "post",
            data: { add: UG, remove: "[]" },
            success: function (text, flag, res) {
                grid.unmask();
                if (res.responseText == "") {
                    grid.reload();
                } else
                    top.mini.alert(res.responseText);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                mini.alert(errorThrown.responseText);
            }
        });
    }

    function remove() {
        var list = grid.getSelecteds();
        if (!list) {
            $.tip({ text: "请选择至少一名用户", staytime: 1000 });
            return;
        }

        var UG = '[';
        for (var i = 0, l = list.length; i < l; i++) {
            UG += '{"UID":"' + list[i].UID + '","GID":"' + mini.get("gid").getValue() + '","_state":"removed"},';
        }
        if (UG.length > 1) {
            UG = UG.substr(0, UG.length - 1);
        }
        UG += ']';

        var add = mini.encode(UG);

        grid.loading("正在保存");
        $.ajax({
            url: "/SystemData/Group/SaveGroupUser",
            type: "post",
            data: { remove: UG, add: "[]" },
            success: function (text, flag, res) {
                grid.unmask();
                if (res.responseText == "") {
                    grid.reload();
                } else
                    top.mini.alert(res.responseText);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                mini.alert(errorThrown.responseText);
            }
        });
    }
</script>


